package com.tsd.system.controller;

import com.alibaba.fastjson.JSON;
import com.tsd.CommonParamNameConstants;
import com.tsd.common.controller.BaseController;
import com.tsd.core.constants.Constants;
import com.tsd.core.datasource.DataSourceContextHolder;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.utils.HttpUtil;
import com.tsd.core.vo.ErrorCode;
import com.tsd.core.vo.HttpResult;
import com.tsd.system.entity.SysUser;
import com.tsd.system.service.SyncDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：同步数据 控制层
 *
 * @author david
 * @date 2021/07/08 15:49:18
 */
@Controller
@Scope("prototype")
@RequestMapping("/api/v2")
@Api(tags = "系统-数据对象", description = "数据对象的API描述")
public class SyncDataController extends BaseController {

    @Resource
    private SyncDataService syncDataService;

    @ApiOperation(value = "保存数据对象", notes = "保存数据对象，具有id值则更新，没有id值则新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "obj", value = "数据对象", required = true, paramType = "body", dataType = "SysDataObj")
    })
    @ResponseBody
    @RequestMapping(value = "/saveSyncDataList.action", method = RequestMethod.POST)
    public HttpResult<String> saveSyncDataList() {
        HttpResult<String> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            String account_set_sid = HttpUtil.getAccountSet(request);
            String table_name = HlpUtils.getString(map, CommonParamNameConstants.TABLE_NAME);
            String json = HlpUtils.getString(map, CommonParamNameConstants.LIST);
            List<Map<String, Object>> t = JSON.parseObject(json, Constants.LIST_MAP_STRING_OBJECT_TYPE_REFERENCE);
            DataSourceContextHolder.setDataSourceType(null);
            syncDataService.saveSyncDataList(account_set_sid, table_name, t, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "查询需要同步的数据")
    @ResponseBody
    @RequestMapping(value = "/findSyncSrcList.action", method = RequestMethod.POST)
    public HttpResult<Object> findSyncSrcList() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<>(2);
            DataSourceContextHolder.setDataSourceType(null);
            HlpUtils.copyStringValue(map, params, Constants.HEADER_NAME_ACCOUNT_SET_SID, Constants.HEADER_NAME_ACCOUNT_SET_SID, true, false);
            HlpUtils.copyStringValue(map, params, CommonParamNameConstants.TABLE_NAME, CommonParamNameConstants.TABLE_NAME, true, false);
            List<Map<String, Object>> list = syncDataService.findSyncSrcList(params);
            hr.setData(list);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

}
